package controllers

import (
	"net/http"

	"gitee.com/kun_liu/to-log/app/services"
	"github.com/gin-gonic/gin"
)

// HomeController 首页控制器
type HomeController struct {
	postService     *services.PostService
	pageService     *services.PageService
	categoryService *services.CategoryService
	tagService      *services.TagService
	commentService  *services.CommentService
	optionService   *services.OptionService
}

// NewHomeController 创建首页控制器实例
func NewHomeController() *HomeController {
	return &HomeController{
		postService:     services.NewPostService(),
		pageService:     services.NewPageService(),
		categoryService: services.NewCategoryService(),
		tagService:      services.NewTagService(),
		commentService:  services.NewCommentService(),
		optionService:   services.NewOptionService(),
	}
}

// Index 首页
func (c *HomeController) Index(ctx *gin.Context) {
	// 获取最新文章列表
	posts, _, err := c.postService.GetPosts(1, 10)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	// 获取分类列表
	categories, err := c.categoryService.GetCategories()
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	// 获取站点标题
	title, err := c.optionService.GetOption("title")
	if err != nil {
		title = "ToLog博客"
	}

	ctx.HTML(http.StatusOK, "index.html", gin.H{
		"title":      title,
		"posts":      posts,
		"categories": categories,
	})
}

// Archive 归档页
func (c *HomeController) Archive(ctx *gin.Context) {
	// 获取所有已发布文章
	posts, _, err := c.postService.GetPosts(1, 100) // 设置较大的页面大小以获取更多文章
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	ctx.HTML(http.StatusOK, "archive.html", gin.H{
		"title": "文章归档",
		"posts": posts,
	})
}

// Category 分类页
func (c *HomeController) Category(ctx *gin.Context) {
	slug := ctx.Param("slug")

	// 获取分类信息
	category, err := c.categoryService.GetCategoryBySlug(slug)
	if err != nil {
		ctx.HTML(http.StatusNotFound, "404.html", gin.H{
			"title": "分类不存在",
		})
		return
	}

	// 获取分类下的文章
	posts, _, err := c.postService.GetPostsByCategory(category.ID, 1, 100)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	ctx.HTML(http.StatusOK, "category.html", gin.H{
		"title":    category.Name,
		"category": category,
		"posts":    posts,
	})
}

// Tag 标签页
func (c *HomeController) Tag(ctx *gin.Context) {
	slug := ctx.Param("slug")

	// 获取标签信息
	tag, err := c.tagService.GetTagBySlug(slug)
	if err != nil {
		ctx.HTML(http.StatusNotFound, "404.html", gin.H{
			"title": "标签不存在",
		})
		return
	}

	// 获取标签下的文章
	posts, _, err := c.postService.GetPostsByTag(tag.ID, 1, 100)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	ctx.HTML(http.StatusOK, "tag.html", gin.H{
		"title": tag.Name,
		"tag":   tag,
		"posts": posts,
	})
}

// Post 文章详情页
func (c *HomeController) Post(ctx *gin.Context) {
	slug := ctx.Param("slug")

	// 获取文章信息
	post, err := c.postService.GetPostBySlug(slug)
	if err != nil {
		ctx.HTML(http.StatusNotFound, "404.html", gin.H{
			"title": "文章不存在",
		})
		return
	}

	// 获取文章评论
	comments, _, err := c.commentService.GetCommentsByContentID(post.ID, 1, 100)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	// 更新浏览量
	post.Views++
	_ = c.postService.UpdatePost(&post)

	ctx.HTML(http.StatusOK, "post.html", gin.H{
		"title":    post.Title,
		"post":     post,
		"comments": comments,
	})
}

// Page 页面详情
func (c *HomeController) Page(ctx *gin.Context) {
	slug := ctx.Param("slug")

	// 获取页面信息
	page, err := c.pageService.GetPageBySlug(slug)
	if err != nil {
		ctx.HTML(http.StatusNotFound, "404.html", gin.H{
			"title": "页面不存在",
		})
		return
	}

	// 获取页面评论
	comments, _, err := c.commentService.GetCommentsByContentID(page.ID, 1, 100)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "error.html", gin.H{
			"title": "服务器错误",
			"error": err.Error(),
		})
		return
	}

	// 更新浏览量
	page.Views++
	c.pageService.UpdatePage(&page)

	ctx.HTML(http.StatusOK, "page.html", gin.H{
		"title":    page.Title,
		"page":     page,
		"comments": comments,
	})
}

// NotFound 404页面
func (c *HomeController) NotFound(ctx *gin.Context) {
	ctx.HTML(http.StatusNotFound, "404.html", gin.H{
		"title": "页面不存在",
	})
}
